package com.lmq.interfaces;// interfaces/Adventure.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Multiple interfaces

interface CanFight {
  void fight();
}

interface CanSwim {
  void swim();
}

interface CanFly {
  void fly();
}

class ActionCharacter {
  public void fight() {}
}

class Hero extends ActionCharacter
    implements CanFight, CanSwim, CanFly {
  @Override public void swim() {}
  @Override public void fly() {}
}

public class Adventure {
  public static void t(CanFight x) { x.fight(); }
  public static void u(CanSwim x) { x.swim(); }
  public static void v(CanFly x) { x.fly(); }
  public static void w(ActionCharacter x) { x.fight(); }
  public static void main(String[] args) {
    Hero h = new Hero();
    t(h); // Treat it as a CanFight
    u(h); // Treat it as a CanSwim
    v(h); // Treat it as a CanFly
    w(h); // Treat it as an ActionCharacter
  }
}
/**
 * Hero combines the concrete class ActionCharacter with the interfaces
 * CanFight, CanSwim, and CanFly.When you combine a concrete  class
 * with interfaces this way, the concrete class must come first then the interfaces.(The compiler gives an error
 * otherwise.)
 * The signature for fight() is the same in the interface CanFight and the class ActionCharacter,
 * and fight() is not provided with a definition in Here.You can extend an interface,but then
 * you've got another interface.When you want to create an object, all the
 * definitions must first exist.Even though Hero does not explicitly provide a definition for fight()
 * The definition comes along with ActionCharacter;thus it's possible to create Hero objects.
 *
 *
 * Adventure show four methods that take arguments of the various interfaces and
 * of the concrete class.When you create a Hero object, it can be passed to any of these methods,
 * which means it is upcast to each interface in turn.Because of the way interface are designed in java,this work without any paritcular effort on the
 * part of the programmer.
 * Keep in mind that one of the core reasons for interfaces is shown
 * in the preceding example:to upcast to more than one base type
 * (and the flexibility this provides).
 * However, a second reason for using interfaces is the same as using an abstract base class:
 * to prevent the client programmer from making an object of this class and
 * to establish it  is only an interface.
 * This brings up a question:Should you use an interface or an abstract class?
 * If  it's possible to create your base class with any method definitions or member variables, prefer interfaces to
 * abstract classes.In fact, if you know something is a base class, you
 * can consider making it an interface(this subject is revisited in the chapter summary)
 * //这就带来一个问题，你应该用接口还是抽象类呢？
 * 如果创建你的基类没有任何的方法定义或者成员变量，那就使用接口好些。实际上，如果你知道
 * 有些东西是一个基类，你可以考虑把它做成一个接口（这个话题会在后面再次谈到）。
 *
 */


// Extending an interface with inheritance
/**
 * You can easily add new method declarations to an interface by using inheritance, and you can also
 * combine several interfaces into a new interface with inheritance
 * In both cases you get a new interface, as seen in this example.
 */
